<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="Activity name" prop="name">
      <el-input v-model="ruleForm.name" placeholder="Please input" clearable />
    </el-form-item>
    <el-form-item label="Activity zone" prop="age">
      <el-input v-model="ruleForm.age" placeholder="Please input" clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm"> Create </el-button>
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
      <el-button @click="addRule()">addRule</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { reactive, ref } from "vue";

const ruleFormRef = ref();
const ruleForm = reactive({
  name: "",
  age: "",
  count: "",
  date1: "",
  date2: "",
  delivery: false,
  location: "",
  type: [],
  resource: "",
  desc: "",
});

const rules = reactive({
  name: [
    {
      required: true,
      message: "Please input Activity name",
      trigger: "blur",
    },
  ],
  age: {
    required: true,
  },
});

const submitForm = async () => {
  ruleFormRef.value.validate((isValid) => {
    if (!isValid) {
      ElMessage.error({
        message: "错误",
      });
      console.log("123");
      return false;
    }
  });
  console.log("form表单的validate方法是异步执行的，这里无论如何都会执行的");
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

const addRule = () => {};
</script>
